export const validatNumber = name => (rule, value, callback) => {
    const reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{0,3})?$/;
    value = (value || '0').toString();
    if (value && value.slice(0, 1) === '-') {
        value = value.slice(1);
    }

    if (!value || (value && reg.test(value))) {
        callback();
    } else {
        return callback(new Error(name + '应为0-3位小数的数字'));
    }
};

export const validatString = count => (rule, value, callback) => {
    value = (value || '').toString();

    if (value.length >= 0 && value.length <= count) {
        callback();
    } else {
        return callback(new Error(`长度限制为${count}个字符`));
    }
};

export default function getRules() {
    const RULES = {
        name: [
            {required: true, message: '账户名称必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        number: [
            {required: true, message: '账户编号必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        outStart: [
            // {required: false, message: '期初金额必填项', trigger: ['change', 'blur']},
            {validator: validatNumber('期初金额'), trigger: ['change', 'blur']}
        ],
        outEnd: [
            // {required: false, message: '当前余额必填项', trigger: ['change', 'blur']},
            {validator: validatNumber('当前余额'), trigger: ['change', 'blur']}
        ],
        remark: [{validator: validatString(200), trigger: ['change', 'blur']}]
    };

    return RULES;
}
